/**
 * 商品类目数据模型
 * @type {GoodsCategory}
 */
/*********************/
module.exports = class GoodsCategory extends require('./model') {

  /**
   * 插入商品类目
   * @param goods_category 商品类目信息
   * @returns {Promise<插入商品类目>}
   */
  static setGoodsCategory(goods_category) {
    return new Promise((resolve, reject) => {
      let sql = 'INSERT INTO goods_category SET ?'

      console.log('插入商品类目sql=' + sql);
      console.log('name=' + goods_category.name + ';describe=' + goods_category.describe + ';root_category_id=' + goods_category.root_category_id
         + ';user_username=' + goods_category.user_username + ';index=' + goods_category.index + ';--------------------------');

      this.dbQuery(sql, goods_category)
        .then(result => {
          resolve(result.insertId);
        })
        .catch(error => {
          console.log('插入商品类目失败！！！');
          reject(error);
        })
    })
  }

  /**
   * 删除商品类目
   * @param id
   * @returns {Promise<删除商品类目>}
   */
  static deleteGoodsCategory(id) {
    return new Promise((resolve, reject) => {
      let sql = `DELETE FROM goods_category WHERE id = ${id}`

      console.log('删除商品类目的sql=' + sql);
      console.log('id=' + id + ';--------------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result.affectedRows)
        })
        .catch(error => {
          reject(error)
        })
    })
  }

  /**
   * 修改商品类目信息
   * @param goods_category
   * @returns {Promise<unknown>}
   */
  static updateGoodsCategory(goods_category) {
    return new Promise((resolve, reject) => {
      let sql = `UPDATE goods_category SET goods_category.name = '${goods_category.name}', goods_category.describe = '${goods_category.describe}', 
        goods_category.root_category_id = ${goods_category.root_category_id}, goods_category.user_username = '${goods_category.user_username}', 
        goods_category.index = ${goods_category.index} WHERE goods_category.id = ${goods_category.id}`

      console.log('更改商品类目信息sql=' + sql);
      console.log('name = ' + goods_category.name + '; describe = ' + goods_category.describe + '; root_category_id = ' +
        goods_category.root_category_id + '; user_username = ' + goods_category.user_username + '; index = ' +
        goods_category.index + '; id = ' + goods_category.id);

      this.dbQuery(sql)
        .then(result => {
          resolve(result.affectedRows)
        })
        .catch(error => {
          console.log('更改商品类目信息失败！！！');
          reject(error)
        })
    })
  }

  /**
   * 获取指定的商品类目条数
   * @param id 商品类目id
   * @param name_describe 商品类目名、描述
   * @param username 商品类目创建人
   * @param root_category_id 商品类目的root类目
   * @returns {Promise<获取指定的商品类目条数>}
   */
  static getGoodsCategoryCount(id, name_describe, username, root_category_id) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT COUNT(1) AS count  
                  FROM goods_category, root_category 
                  WHERE (1 = 1 AND goods_category.root_category_id = root_category.id) `;
      sql += (id != 0 && id) ? ` AND goods_category.id = ${id} ` : '';
      sql += (username != '' && username) ? ` AND goods_category.user_username = ${username} ` : '';
      sql += (root_category_id != 0 && root_category_id) ? ` AND goods_category.root_category_id = ${root_category_id} ` : '';

      sql += (name_describe != '' && name_describe) ? ` AND (goods_category.name LIKE '%${name_describe}%' OR ` : '';
      sql += (name_describe != '' && name_describe) ? ` goods_category.describe LIKE '%${name_describe}%') ` : '';

      console.log('商品类目数量sql' + sql);
      console.log('id=' + id + ';username=' + username + ';name_describe=' + name_describe +
        ';root_category_id=' + root_category_id + ';----------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result[0]);
        })
        .catch(error => {
          reject(error);
        })
    })
  }

  /**
   * 查询商品类目信息
   * @param start 开始记录
   * @param size 获取条数
   * @param id 商品类目id
   * @param name_describe 商品类目名、描述
   * @param username 商品类目创建者的username
   * @param root_category_id 商品类目所属的root_id
   * @returns {Promise<查询的商品类目记录信息>}
   */
  static getGoodsCategory(start, size, id, name_describe, username, root_category_id) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT goods_category.*, root_category.name root_category_name 
                  FROM goods_category, root_category 
                  WHERE (1 = 1 AND goods_category.root_category_id = root_category.id) `;
      sql += id != 0 && id ? ` AND goods_category.id = ${id} ` : ' ';
      sql += username != '' && username ? ` AND goods_category.user_username = ${username} ` : ' ';
      sql += root_category_id != 0 && root_category_id ? ` AND goods_category.root_category_id = ${root_category_id} ` : ' ';

      sql += name_describe != '' && name_describe ? ` AND (goods_category.name LIKE '%${name_describe}%' OR ` : ' ';
      sql += name_describe != '' && name_describe ? ` goods_category.describe LIKE '%${name_describe}%') ` : ' ';

      sql += ` ORDER BY goods_category.index DESC LIMIT ?, ? `;

      console.log('商品类目信息sql' + sql);
      console.log('id=' + id + ';name_describe=' + name_describe + ';username=' + username +
        ';root_category_id=' + root_category_id + ';start=' + start + ';size=' + size + ';----------------');

      this.dbQuery(sql, [start, size])
        .then(result => {
          resolve(result);
        })
        .catch(error => {
          reject(error);
        })
    })
  }


}
